<template>
  <div class="mod-supplier-supplier">
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :table-loading="dataListLoading"
               :option="tableOption"
               @search-change="searchChange"
               @on-load="getDataList"
               @refresh-change="refreshChange"
    >
      <template slot-scope="scope"
                slot="menuLeft">
        <el-button
          icon="el-icon-plus"
          type="primary"
          size="small"
          v-if="isAuth('supplier:supplier:save')"
          @click="addOrUpdateHandle()">{{$t("product.addNew")}}
        </el-button>
      </template>
      <template slot-scope="scope"
                slot="menu">
        <el-button
          icon="el-icon-edit"
          size="small"
          type="primary"
          v-if="isAuth('supplier:supplier:update')"
          @click="addOrUpdateHandle(scope.row.supplierId)">{{$t("temp.modify")}}
        </el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          v-if="isAuth('supplier:supplier:delete')"
          @click="deleteHandle(scope.row.supplierId)">{{$t("text.delBtn")}}
        </el-button>
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible"
                   ref="addOrUpdate"
                   @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
  import {tableOption} from '@/crud/shop/supplier'
  import AddOrUpdate from './supplier-add-or-update'

  export default {
    data () {
      return {
        dataList: [],
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 10 // 每页显示多少条
        },
        searchForm: {}, // 搜索
        dataListLoading: false,
        tableOption: tableOption,
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    methods: {
      getDataList (page) {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/supplier/supplier/page'),
          method: 'get',
          params: this.$http.adornParams(
            Object.assign(
              {
                current: page == null ? this.page.currentPage : page.currentPage,
                size: page == null ? this.page.pageSize : page.pageSize
              },
              this.searchForm
            )
          )
        }).then(({data}) => {
          data.records.forEach(row => {
            row.status = row.status === 1 ? this.$i18n.t('shop.ena') : this.$i18n.t('publics.disable')
          })

          this.dataList = data.records
          this.page.total = data.total
          this.dataListLoading = false
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      deleteHandle (id) {
        this.$confirm(this.$i18n.t('admin.isDeleOper') + '?', this.$i18n.t('text.tips'), {
          confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
          cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/supplier/supplier/' + id),
            method: 'delete',
            data: this.$http.adornData({})
          }).then(({data}) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.refreshChange()
              }
            })
          })
        }).catch(() => {
        })
      },
      /**
       * 刷新回调
       */
      refreshChange () {
        this.page = this.$refs.crud.$refs.tablePage.defaultPage
        this.getDataList(this.page)
      },
      searchChange (params) {
        console.log(params)
        this.searchForm = params
        this.getDataList(this.page)
      }
    }
  }
</script>
<style lang="scss">
</style>
